package com.meitianhui.convenitenbuy.module.order.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.order.adapter.FightGroupOrderDetailAdapter;
import com.meitianhui.convenitenbuy.module.order.contract.FightGroupOrderDetailContract;
import com.meitianhui.convenitenbuy.module.order.di.DaggerOrderComponent;
import com.meitianhui.convenitenbuy.module.order.di.OrderModule;
import com.meitianhui.convenitenbuy.module.order.model.bean.FightGroupDetailPerson;
import com.meitianhui.convenitenbuy.module.order.model.bean.FightGroupOrderDetail;
import com.meitianhui.convenitenbuy.module.order.presenter.FightGroupOderDetailPresenter;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONObject;

import java.util.HashMap;

import javax.inject.Inject;

/**
 * Created by Administrator on 2017/11/22.
 */

public class FightGroupOrderDetailActivity extends BaseToolbarActivity implements FightGroupOrderDetailContract.View{

    private RecyclerView mRecyclerView;
    private String mOrderId,mActivityId;
    private int mPageIndex = 1;
    private int mPageSize = 15;
    private FightGroupOrderDetailAdapter mAdapter;
    private View mView;
    //head
    private ImageView mIvGood;
    private TextView mTvOrderNumber,mTvDepositFee,mTvLadderPrice,mTvPayWay,mTvCreateTime;
    private TextView mTvStoreName,mTvStatus,mTvContactPeople,mTvOpenPeople,mTvPeopleNum;
    private TextView mTvAddressTitle,mTvAddress,mTvReceivedWay,mTvGoodName;
    private LinearLayout mLlStoreName;

    @Inject
    FightGroupOderDetailPresenter presenter;

    @Override
    protected int setView() {
        return R.layout.activity_fight_group_order_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerOrderComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .orderModule(new OrderModule(this))
                .build()
                .inject(this);
        setTbTitle("详情");
        isEnable(false);
        mRecyclerView = getViewById(R.id.rv_fight_group_detail);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mView = getLayoutInflater().inflate(R.layout.item_fight_group_order_detail_head, (ViewGroup) mRecyclerView.getParent(), false);
        initHeadView();
        initData();
    }

    private void initHeadView(){
        mIvGood = (ImageView) mView.findViewById(R.id.iv_good);
        mTvOrderNumber = (TextView) mView.findViewById(R.id.tv_order_number);
        mTvDepositFee = (TextView) mView.findViewById(R.id.tv_deposit_fee);  //预付金额
        mTvLadderPrice = (TextView) mView.findViewById(R.id.tv_ladder_price);  //成团价格
        mTvPayWay = (TextView) mView.findViewById(R.id.tv_pay_way);  //支付方式
        mTvCreateTime = (TextView) mView.findViewById(R.id.tv_create_time);  //创建时间
        mTvStoreName = (TextView) mView.findViewById(R.id.tv_store_name);  //商店名称
        mTvStatus = (TextView) mView.findViewById(R.id.tv_status);  //状态
        mTvContactPeople = (TextView) mView.findViewById(R.id.tv_contact_people);  //联系人
        mTvOpenPeople = (TextView) mView.findViewById(R.id.tv_open_people);  //发起人
        mTvPeopleNum = (TextView) mView.findViewById(R.id.tv_people_num);  //总数
        mLlStoreName = (LinearLayout) mView.findViewById(R.id.ll_store_name);
        mTvAddressTitle = (TextView) mView.findViewById(R.id.tv_address_title);
        mTvAddress = (TextView) mView.findViewById(R.id.tv_address);
        mTvReceivedWay = (TextView) mView.findViewById(R.id.tv_received_way);
        mTvGoodName = (TextView) mView.findViewById(R.id.tv_good_name);
    }

    private void initData(){
        mOrderId = getIntent().getStringExtra("order_id");
        mActivityId = getIntent().getStringExtra("activity_id");
        mAdapter = new FightGroupOrderDetailAdapter(this,null,null);
        mAdapter.addHeaderView(mView);
        mRecyclerView.setAdapter(mAdapter);
        presenter.getOrderDetail(mOrderId);

    }

    @Override
    public void getOrderDetailSuccess(FightGroupOrderDetail data) {
        if(data!=null){
            setHeadData(data);
        }
        presenter.getOrderPerson(mActivityId,mPageIndex+"",mPageSize+"");

    }




    private void setHeadData(FightGroupOrderDetail data){
        mTvOrderNumber.setText("订单编号 " + data.getDetail().getOrder_no());
        HashMap<String,String> hashMap=data.getDoc_url();
        mTvDepositFee.setText("¥"+data.getDetail().getDeposit_fee());
        mTvLadderPrice.setText("¥" + data.getDetail().getActivity_ladder_price());
        mTvPayWay.setText(data.getDetail().getPayWay());
        mTvCreateTime.setText(data.getDetail().getOrder_date());
        mTvStoreName.setText(data.getDetail().getStores_name());
        mTvStatus.setText(data.getDetail().getOrderStatus());
        //联系人
        mTvContactPeople.setText(data.getDetail().getContact_person()+"  "+data.getDetail().getContact_tel());
        mTvOpenPeople.setText(data.getDetail().getMember_mobile());//发起人
        String receivedMode="";
        if("pick_up".equals(data.getDetail().getReceived_mode())){
            receivedMode="门店自提";
            mLlStoreName.setVisibility(View.VISIBLE);
            mTvAddressTitle.setText("收货地址");
        }else {
            receivedMode="送货上门";
            mLlStoreName.setVisibility(View.GONE);
            mTvAddressTitle.setText("收货地址");
        }
        //门店名称也隐藏
        mLlStoreName.setVisibility(View.GONE);
        //收货地址 展示详细地址＋门店名称＋联系人＋联系方式
        mTvAddress.setText(data.getDetail().getAddress()+"( "+data.getDetail().getStores_name()+"  "+data.getDetail().getContact_person()+"  " +data.getDetail().getContact_tel()+" )");
        mTvReceivedWay.setText(receivedMode);
        mTvGoodName.setText(data.getDetail().getDesc1());
        if(hashMap!=null) {
            String picInfo = data.getDetail().getGoods_json_data();
            if (!TextUtils.isEmpty(picInfo)) {
                try {
                    JSONObject jsonObject = new JSONObject(picInfo);
                    String pathId = jsonObject.getString("path_id");
                    pathId = hashMap.get(pathId);
                    GlideUtils.setRectImage(this,pathId,R.mipmap.icon_home_service_default,R.mipmap.icon_home_service_default,mIvGood);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    public void getOrderDetailFail(int code, String msg) {

    }

    @Override
    public void getOrderPersonSuccess(FightGroupDetailPerson data) {
        String  total = data.getPage().getTotal_count()+"";
        if(!TextUtils.isEmpty(total)) {
            mTvPeopleNum.setText(total + "人参团");
        }
        mAdapter.setNewData(data.getList(),data.getDoc_url());
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }

}
